namespace Crux.Core.Domains;
/// <summary>
/// InjectServiceProviderExtensions
/// </summary>
public static class InjectServiceProviderExtensions
{
    /// <summary>
    /// 触发事件
    /// </summary>
    /// <param name="provider"></param>
    /// <param name="domainEvent"></param>
    /// <exception cref="NotImplementedException"></exception>
    public static void Emit(this IInjectServiceProvider provider, IDomainEvent domainEvent)
    {
        var bus = provider?.ServiceProvider?.GetService(typeof(IDomainEventBus)) as IDomainEventBus;
        if (bus is null)
        {
            throw new NotImplementedException("IDomainEventBusIsNotImplemented");
        }

        bus.Post(domainEvent);
    }

    /// <summary>
    /// 使用内置provider
    /// </summary>
    /// <param name="provider"></param>
    /// <param name="serviceProvider"></param>
    public static void UseServiceProvider(this IInjectServiceProvider provider, IServiceProvider serviceProvider)
    {
        provider.ServiceProvider = serviceProvider;
    }
}